package com.spurdo.kuvapankki;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.vaadin.terminal.FileResource;
import com.vaadin.terminal.StreamResource;
import com.vaadin.terminal.StreamResource.StreamSource;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.ProgressIndicator;
import com.vaadin.ui.Upload;
import com.vaadin.ui.Upload.Receiver;
import com.vaadin.ui.Upload.SucceededEvent;
import com.vaadin.ui.Upload.SucceededListener;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window.Notification;

public class Qva extends CustomComponent {
	private static final long serialVersionUID = -4292553844521293140L;

	public void init(String context) {
		VerticalLayout layout = new VerticalLayout();

		if ("basic".equals(context))
			basic(layout);
		else
			layout.addComponent(new Label("Invalid context: " + context));

		setCompositionRoot(layout);
	}

	void basic(VerticalLayout layout) {
		// BEGIN-EXAMPLE: component.upload.basic
		// Create the upload with a caption and set receiver later
		Upload upload = new Upload("", null);
		upload.addStyleName("uploadi");
		upload.setButtonCaption("Start Upload");
		upload.setHeight("100px");

		// Put the upload component in a panel
		final Panel panel = new Panel("");
		panel.setScrollable(true);
		panel.addComponent(upload);
		

		// Show uploaded file in this placeholder
		final Embedded image = new Embedded("Uploaded Image");
		image.setVisible(false);
		final ProgressIndicator indicator =
		        new ProgressIndicator(new Float(0.0));
		indicator.setWidth("0%");

		panel.addComponent(image);
		
		

		// Implement both receiver that saves upload in a file and
		// listener for successful upload
		class ImageUploader implements Receiver, SucceededListener {
			private static final long serialVersionUID = -1276759102490466761L;

			public File file;

			public OutputStream receiveUpload(String filename, String mimeType) {
				 
				indicator.setWidth("100%");
				panel.addComponent(indicator);
				// Create upload stream
				FileOutputStream fos = null; // Output stream to write to
				try {
					// Open the file for writing.
					file = new File("D:\\Qva\\b" + filename);
					fos = new FileOutputStream(file);
					
				} catch (final java.io.FileNotFoundException e) {
					getWindow().showNotification("Could not open file<br/>",
							e.getMessage(), Notification.TYPE_ERROR_MESSAGE);
					return null;
				}
				indicator.setValue(new Float(0.5));
				return fos; // Return the output stream to write to
			}

			public void uploadSucceeded(SucceededEvent event) {
				// Show the uploaded file in the image viewer
				image.setVisible(true);
				image.setSource(new FileResource(file, getApplication()));
				indicator.setValue(new Float(1.0));
				indicator.setWidth("0%");
			}
		}

		final ImageUploader uploader = new ImageUploader();
		upload.setReceiver(uploader);
		upload.addListener(uploader);
		// END-EXAMPLE: component.upload.basic

		// Create uploads directory
		File uploads = new File("D:\\Qva\\b");

		if (!uploads.exists() && !uploads.mkdir())
			layout.addComponent(new Label("ERROR: Could not create upload dir"));

		((VerticalLayout) panel.getContent()).setSpacing(true);

		panel.setWidth("100%");

		layout.addComponent(panel);
	}

}
